package com.beta.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.beta.service.ServiceException;
import com.beta.dto.version.VersionInfoDto;
import com.beta.entity.VersionInfos;
import com.beta.mapper.VersionInfosMapper;
import com.beta.service.IDateTimeService;
import com.beta.service.IVersionInfosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * VersionInfos 表数据服务层接口实现类
 */
@Service
public class VersionInfosService extends SuperServiceImpl<VersionInfosMapper, VersionInfos> implements IVersionInfosService {
    @Autowired
    private IDateTimeService dbService;
    @Autowired
    private VersionInfosMapper versionInfosMapper;

    @Override
    public VersionInfos getLatestVersionInfo() {
        return versionInfosMapper.getLatestVersion();
    }

    @Override
    public Page<VersionInfos> getVersionInfos(Page<VersionInfos> page) {
        return selectPage(page,new EntityWrapper<>(new VersionInfos()).orderBy("id",false));
    }

    @Override
    public VersionInfos getVersionInfo(Integer id) {
        if(Objects.isNull(id)){
            throw new ServiceException("versionInfos.invalid.id");
        }
        return selectById(id);
    }

    @Override
    public void updateVersionInfo(Integer id, VersionInfoDto versionDto) {
        if(Objects.isNull(id)){
            throw new ServiceException("versionInfos.invalid.id");
        }
        VersionInfos versionInfos = new VersionInfos().setVersion(versionDto.getVersion())
                                    .setForceUpdate(versionDto.getForceUpdate())
                                    .setIosUrl(versionDto.getIosUrl())
                                    .setAndroidUrl(versionDto.getAndroidUrl())
                                    .setContent(versionDto.getContent())
                                    .setForceUpdate(versionDto.getForceUpdate())
                                    .setUpdatedAt(dbService.toDbDate());
        updateSelective(versionInfos,new VersionInfos().setId(id));
    }

    @Override
    public void createVersionInfo(VersionInfoDto versionDto) {
        VersionInfos versionInfos = new VersionInfos().setVersion(versionDto.getVersion())
                .setForceUpdate(versionDto.getForceUpdate())
                .setIosUrl(versionDto.getIosUrl())
                .setAndroidUrl(versionDto.getAndroidUrl())
                .setContent(versionDto.getContent())
                .setForceUpdate(versionDto.getForceUpdate())
                .setCreatedAt(dbService.toDbDate())
                .setUpdatedAt(dbService.toDbDate());
        insertSelective(versionInfos);
    }

    @Override
    public void deleteVersionInfo(Integer id) {
        if(Objects.isNull(id)){
            throw new ServiceException("versionInfos.invalid.id");
        }
        deleteById(id);
    }
}